package com.zouxin.zx_recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class StartDecoration extends RecyclerView.ItemDecoration {
    private int groupHeaderHeight;

    private Paint headPaint;
    private Paint textPaint;
    private Paint linePaint;

    private Rect textRect;

    public StartDecoration(Context context) {
        headPaint = new Paint();
        textPaint = new Paint();
        linePaint = new Paint();
        headPaint.setColor(Color.RED);
        textPaint.setColor(Color.BLACK);
        linePaint.setColor(Color.YELLOW);
        textPaint.setTextSize(50);
        textRect = new Rect();
        groupHeaderHeight = dp2px(context, 100);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (parent.getAdapter() instanceof StarAdapter) {
            //如果是头部，预留更大的地方
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            int childCount = parent.getChildCount();
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            for (int i = 0; i < childCount; i++) {
                //获取对应i的View
                View childView = parent.getChildAt(i);
                //获取View的布局位置
                int position = parent.getChildLayoutPosition(childView);
                //是否是头部
                boolean isGroupHeader = adapter.isGourpHeader(position);
                if (isGroupHeader&&childView.getTop()-groupHeaderHeight-parent.getPaddingTop()>=0) {
                    c.drawRect(left, childView.getTop() - groupHeaderHeight, right, childView.getTop(), headPaint);
                    String groupName = adapter.getGroupName(position);
                    textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);
                    //textRect.height()/2用来将文字的高度除以2分之一使其居中，groupHeaderHeight/2获取高度的二分之一
                    c.drawText(groupName, left + 15, childView.getTop() - groupHeaderHeight / 2 + textRect.height() / 2, textPaint);
                } else if (childView.getTop()-groupHeaderHeight-parent.getPaddingTop()>=0){
                    //分割线
                    c.drawRect(left, childView.getTop() - 3,
                            right, childView.getTop(), linePaint);
                }
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if (parent.getAdapter() instanceof StarAdapter) {
            //如果是头部，预留更大的地方
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            //返回可见区域的第一个item的postion
            int itemPosition = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
            //通过观看源码可知这里拿到的itemView就是适配器里的View view = LayoutInflater.from(context).inflate(R.layout.rv_item_start, null);
            View itemView = parent.findViewHolderForAdapterPosition(itemPosition).itemView;

            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            int top = parent.getPaddingTop();
            //当第二个是组的头部的时候
            boolean isGourpHeader = adapter.isGourpHeader(itemPosition+1);
            if (isGourpHeader&&itemView.getBottom()-groupHeaderHeight/2-textRect.height() / 2-parent.getPaddingTop()>=0){
                Config.d("onDrawOver","itemView.getBottom()-groupHeaderHeight/2-textRect.height() / 2-parent.getPaddingTop====="+(itemView.getBottom()-groupHeaderHeight/2-textRect.height() / 2-parent.getPaddingTop()));
                int bottom=Math.min(groupHeaderHeight,itemView.getBottom()-parent.getPaddingTop());
                c.drawRect(left,top,right,top+bottom,headPaint);
                String groupName = adapter.getGroupName(itemPosition);
                textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);
                //textRect.height()/2用来将文字的高度除以2分之一使其居中，groupHeaderHeight/2获取高度的二分之一
                c.drawText(groupName, left + 15, top+bottom - groupHeaderHeight / 2 + textRect.height() / 2, textPaint);
            }else if(isGourpHeader&&itemView.getBottom()-groupHeaderHeight/2-textRect.height() / 2-parent.getPaddingTop()>=-((groupHeaderHeight / 2) + (textRect.height() / 2))){
                c.drawRect(left,top,right,groupHeaderHeight+top,headPaint);
                String groupName = adapter.getGroupName(itemPosition+1);
                textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);
                //textRect.height()/2用来将文字的高度除以2分之一使其居中，groupHeaderHeight/2获取高度的二分之一
                c.drawText(groupName, left + 15, top + groupHeaderHeight / 2 + textRect.height() / 2, textPaint);

            }
            else  {
                c.drawRect(left,top,right,groupHeaderHeight+top,headPaint);
                String groupName = adapter.getGroupName(itemPosition);
                textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);
                //textRect.height()/2用来将文字的高度除以2分之一使其居中，groupHeaderHeight/2获取高度的二分之一
                c.drawText(groupName, left + 15, top + groupHeaderHeight / 2 + textRect.height() / 2, textPaint);
            }
        }
    }

    //用来预留空间
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        if (parent.getAdapter() instanceof StarAdapter) {
            //如果是头部，预留更大的地方
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            //判断itemView是头部
            int childLayoutPosition = parent.getChildLayoutPosition(view);
            boolean isHeader = adapter.isGourpHeader(childLayoutPosition);
            if (isHeader) {
                outRect.set(0, groupHeaderHeight, 0, 0);
            } else {
                //1像素
                outRect.set(0, 3, 0, 0);
            }
        }
    }

    //我们在做安卓屏幕适配的时候，根据手机的分辨率从 dp 的单位 转成为 px(像素)
    private int dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale * 0.55f);
    }
}